﻿using System;
using System.Data;
using BCL.Entity;
using NazSoft.Framework;
using System.Linq;
using NazSoft.Framework.Extensions;
namespace BCL.Repository
{
    public class ImageInformationRepository
    {
        public int SaveLogoInformation(FileInformation fileInformation)
        {
            object[] objName = { "ActualFileName", "CurrentFileName", "FileSize", "FileExtension", "IsActive", 
                                   "IsDeleted", "CreatedBy", "CreatedDate", "IsFolder", "ParentId" };
            object[] objValue = { fileInformation.ActualFileName, fileInformation.CurrentFileName, fileInformation.FileSize, fileInformation.FileExtension,
                                    fileInformation.IsActive,fileInformation.IsDeleted,fileInformation.CreatedBy,
                                    fileInformation.CreatedDate,fileInformation.IsFolder, fileInformation.ParentId};

            var parameter = new System.Data.SqlClient.SqlParameter("@Id", 0);
            //cmd.Parameters.Add("@Id", SqlDbType.Int, 0, "CategoryID");
            parameter.Direction = ParameterDirection.Output;

            SqlHelper helpclass = new SqlHelper();
            var result = helpclass.ExecuteNonQuery("SP_FileInformation", objName, objValue, (cmd) =>
            {
                cmd.Parameters.Add(parameter);
            });
            int Id = Convert.ToString(parameter.Value).ToInt32Safe();

            return Id;
        }

        public FileInformation Get(int id)
        {
            String cacheKey = "GetImage" + id;


            var data = CacheHelper.Get<FileInformation>(cacheKey);

            if (data == null)
            {
                data = GetFromDatabase(id);

                if (data != null)
                {
                    CacheHelper.Add(data, cacheKey);
                }
            }

            return data;
        }

        public FileInformation GetFromDatabase(int id)
        {
            object[] objName = { "Id" };
            object[] objValue = { id };

            SqlHelper helpclass = new SqlHelper();
            DataSet dataSet = helpclass.GetDataSet("SP_GetFileInformation", objName, objValue);

            FileInformation model = CollectionHelper.ConvertTo<FileInformation>(dataSet.Tables[0]).FirstOrDefault();

            return model;
        }

    }
}
